package com.sun.hunk.hibernate.domain.impl;

import com.sun.hunk.hibernate.domain.Item;
import com.sun.hunk.hibernate.domain.Category;
import com.sun.hunk.hibernate.domain.Comment;
import com.sun.hunk.hibernate.domain.Purchase;
import com.sun.hunk.hibernate.domain.User;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author abhi
 */
@Entity
@Table(name = "item")
public class ItemImpl implements Item {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Long id;    
    @Column(name = "code")
    private String code;
    @Column(name = "description")
    private String description;
    @Column(name = "created_on")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdOn;    
    @Column(name = "quantity")
    private Integer quantity;
    @JoinColumn(name = "category_id", referencedColumnName = "id")
    @ManyToOne(targetEntity=CategoryImpl.class)
    private Category categarory;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "item",targetEntity=CommentImpl.class)
    private List<Comment> comments;
    @OneToOne(targetEntity=UserImpl.class)
    @JoinColumn(name="user_id")
    private User user;
    @OneToMany(mappedBy="item",targetEntity=PurchaseImpl.class)
    private Set<Purchase> purchaseSet;
    @OneToMany(mappedBy="item",targetEntity=SalesImpl.class)
    private Set<Purchase> salesSet;

    public ItemImpl() {
        quantity = 0;
    }

    public ItemImpl(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Date getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    public Category getCategarory() {
        return categarory;
    }

    public void setCategarory(Category topicCategarory) {
        this.categarory = topicCategarory;
    }

    public List<Comment> getComments() {
        return comments;
    }

    public void addComment(Comment comment) {
        if(comments == null){
            comments = new ArrayList<Comment>();
        }
        comment.setItem(this);
        comments.add(comment);
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public Set<Purchase> getPurchaseSet() {
        return purchaseSet;
    }

    public void setPurchaseSet(Set<Purchase> purchaseSet) {
        this.purchaseSet = purchaseSet;
    }

    public Set<Purchase> getSalesSet() {
        return salesSet;
    }

    public void setSalesSet(Set<Purchase> salesSet) {
        this.salesSet = salesSet;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof ItemImpl)) {
            return false;
        }
        ItemImpl other = (ItemImpl) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.sun.hunk.hibernate.domain.[id=" + id + "]";
    }

}
